// 静态路由配置
export const staticRoutes = [
  // 首页模块
  {
    path: '/dashboard',
    component: () => import('@/views/dashboard/index'),
    name: 'Dashboard',
    meta: { title: '首页', icon: 'dashboard', affix: true },
    permissions: []
  },

  // 小说详情页面（独立路由，脱离layout）
  {
    path: '/novel-detail/:id(\\d+)',
    component: () => import('@/views/novel/detail/index'),
    name: 'NovelDetail',
    meta: { title: '小说详情', activeMenu: '/novel/novel' },
    hidden: true
  },

  // 小说管理模块
  {
    path: '/novel',
    component: () => import('@/layout'),
    redirect: '/novel/novel',
    name: 'Novel',
    meta: { title: '小说管理', icon: 'document' },
    children: [
      {
        path: 'novel',
        redirect: '/dashboard',
        name: 'NovelList',
        meta: { title: '小说列表', icon: 'list' },
      },
      {
        path: 'chapter',
        component: () => import('@/views/novel/chapter/index'),
        name: 'Chapter',
        meta: { title: '章节管理', icon: 'edit' },
      },
      {
        path: 'type',
        component: () => import('@/views/novel/type/index'),
        name: 'NovelType',
        meta: { title: '小说类型', icon: 'tree' },
      },
      {
        path: 'tags',
        component: () => import('@/views/novel/tags/index'),
        name: 'NovelTags',
        meta: { title: '小说标签', icon: 'tag' },
      },
      {
        path: 'role',
        component: () => import('@/views/novel/role/index'),
        name: 'NovelRole',
        meta: { title: '人物角色', icon: 'peoples' },
      },
      {
        path: 'storyboard',
        component: () => import('@/views/novel/storyboard/index'),
        name: 'Storyboard',
        meta: { title: '故事板', icon: 'tree-table' },
      }
    ]
  },
]

// 过滤静态路由，根据权限显示
export function filterStaticRoutes(routes, permissions = []) {
  return routes.filter(route => {
    // 检查路由权限
    if (route.permissions && route.permissions.length > 0) {
      const hasPermission = route.permissions.some(permission =>
        permissions.includes(permission)
      )
      if (!hasPermission) {
        return false
      }
    }

    // 递归处理子路由
    if (route.children && route.children.length > 0) {
      route.children = filterStaticRoutes(route.children, permissions)
      // 如果所有子路由都被过滤掉，则隐藏父路由
      if (route.children.length === 0) {
        return false
      }
    }

    return true
  })
}
